package app.poller.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import app.poller.R;
import app.poller.entities.Option;

public class OptionArrayAdapter extends ArrayAdapter<Option> {
	
	ArrayList<Option> optionList;

	public OptionArrayAdapter(Context context, int textViewResourceId, ArrayList<Option> optionList) {
		super(context, textViewResourceId, optionList);
		this.optionList = optionList;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		
		if(convertView == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.option_row, parent, false);
			vh = new ViewHolder();
			vh.optionName = (TextView) convertView.findViewById(R.id.option_name);
			vh.optionDeleteButton = (Button) convertView.findViewById(R.id.option_delete_button);
			convertView.setTag(vh);
		}
		else {
			vh = (ViewHolder) convertView.getTag();
		}
		
		vh.optionName.setText(getItem(position).getOptionName());
		vh.optionDeleteButton.setTag(position);
		vh.optionDeleteButton.setText("delete");
		vh.optionDeleteButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				remove(getItem(((Integer) v.getTag())));
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
	
	public static class ViewHolder {
		TextView optionName;
		Button optionDeleteButton;
	}
}
